package com.hbue.springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hbue.springboot.pojo.Result;
import com.hbue.springboot.pojo.TbHistory;
import com.hbue.springboot.pojo.TbOrder;
import com.hbue.springboot.pojo.TbWorker;
import com.hbue.springboot.service.TbOrderService;
import com.hbue.springboot.service.TbWorkerService;
import com.hbue.springboot.utils.ResultCode;
import com.hbue.springboot.vo.QueryParamsVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/worker/")
@RequiredArgsConstructor
public class TbWorkerController {
    private final TbWorkerService tbWorkerService;

    private final TbOrderService tbOrderService;

    /**
     * 送水工送水订单
     */
    @GetMapping("tick")
    public Result selecttick() {
        tbWorkerService.selectTick();
        return Result.success();
    }

    /**
     *  获取工人可接取的订单
     * @return
     */
    @PostMapping("getOrderListWithoutWorker")
    public Result getOrderWithoutWorker(@RequestBody QueryParamsVo params) {
        IPage<TbHistory> list = tbWorkerService.getOrderWithoutWorker(params);
        if(list != null) {
            return Result.success(list);
        } else {
            return Result.error(ResultCode.NOTFOUND);
        }
    }

    /**
     *  接单
     * @param id
     * @param workerName
     * @return
     */
    @PutMapping("acceptOrder")
    public Result getOrder(@RequestParam("id") Integer id, @RequestParam("workerName") String workerName) {

        Integer res = tbWorkerService.acceptOrder(id, workerName);
        if(res == 1) {
            return Result.success("接单成功");
        } else {
            return Result.error("接单失败");
        }
    }

    /**
     *  获取工人已接取的订单
     * @param params
     * @return
     */
    @PostMapping("getOrderListWithWorker")
    public Result getOrderWithWorker(@RequestBody QueryParamsVo params) {
        log.info("params: {}", params);
        IPage<TbHistory> list = tbWorkerService.getOrderWithWorker(params);
        if(list != null) {
            return Result.success(list);
        } else {
            return Result.error(ResultCode.NOTFOUND);
        }
    }

    @PutMapping("ensureOrder")
    public Result ensureOrder(@RequestParam("id") Integer id) {
        Integer res = tbWorkerService.ensureOrder(id);
        if(res > 0) {
            return Result.success("确认订单成功");
        } else {
            return Result.error("确认订单失败");
        }
    }
}
